package bavaria.hightech.banking.unusedClasses;

import java.util.Iterator;

public class GeneralList_ALT<T> {

	private LinkedElement<T> firstElem;
	private LinkedElement<T> actElem;
	private LinkedElement<T> newElem;
	private int size = 0;
	private Itr<LinkedElement<T>> myIterator;

	public GeneralList_ALT(T subElement) {
		firstElem = new LinkedElement<T>(subElement);
		actElem = firstElem;
		myIterator = new Itr<LinkedElement<T>>(actElem);
	}

	public void add(T subElement) {
		newElem = new LinkedElement<T>(subElement);
		LinkedElement<T> firstElemHelper = firstElem; // firstElemHelper needed
														// so that firstElem is
														// never changed
		newElem.next = firstElemHelper.next;
		firstElemHelper.next = newElem;
		firstElemHelper = newElem;
		size++;
	}

	/**
	 * empties list except first dummy element
	 */
	public void clear() {
		firstElem.next = null;
		actElem = firstElem;
		size = 0;
	}

	/**
	 * puts the "counter" element to the beginning of the list
	 */
	public void resetIteration() {
		actElem = firstElem;
	}

	/**
	 * @return reference to an object from the linked list returns always the
	 *         next booking
	 */
	public LinkedElement<T> next() {
		return myIterator.next();
	}

	private class Itr<E> implements Iterator<E> { // the implementation of the
													// iterator
		private LinkedElement<E> actElem; // internal condition of the iterator

		@SuppressWarnings("unchecked")
		public Itr(LinkedElement<T> actElem) {
			this.actElem = (LinkedElement<E>) actElem;
		}

		@Override
		public boolean hasNext() {
			return actElem.next != null;
		}

		@Override
		@SuppressWarnings("unchecked")
		public E next() {
			if (actElem != null) {
				LinkedElement<E> nextElem = actElem.next;
				actElem = actElem.next;
				return (E) nextElem;
			} else {
				resetIteration();
				return null;
			}
		}

		@Override
		public void remove() {
		}

	}

	public static class LinkedElement<T> {

		private LinkedElement<T> next;
		private final T myElem;

		private LinkedElement(T subElement) {
			myElem = subElement;
		}

		@Override
		public String toString() {
			return myElem.toString();
		}
	}

}
